#include "ds18b20.h"
#include "lcd1602.h"
#include "delay.h"
#include "uart.h"
#include "24c02.h"

#define Desired 40.0
#define Kp 2.0
#define Kd 0.2
#define Ki 0.01

extern unsigned char rec_data;
char status = 1; //0 LCD OFF    1 LCD ON


void main() 
{
	float T = 0.0;
	unsigned char c[8];
	float Current = 0.0;
	float Error = 0.0;
	float Out = 0.0;
	float Error_Last = 0.0;
	float Error_Integral = 0.0;
	char unit;
	int value_save_count = 0;
	unsigned char save_addr = 1;
	
	rec_data = 0;

	Uart_Init();
	LCD_Init();

	unit = eeprom_read_data(0);
	
	while(1) {

		T = ds18b20_read_temper();
		if(unit == 'F') {
			T = 38.0 + T*1.8;
		}
		c[0] = (int)T/10 + 0x30;
		c[1] = (int)T%10 + 0x30;
		c[2] = '.';
		c[3] = (int)(T*10)%10 + 0x30;
		c[4] = (int)(T*100)%10 + 0x30;
		c[5] = unit;
		
		value_save_count++;
		if(value_save_count == 1000) {
			eeprom_write_page(save_addr, c);
			save_addr += 6;
			if(save_addr > 250) {
				save_addr = 1;
			}
		}
		
		//if(status == 1) {
			LCD_Display(c, 1, 0, 6);
		//}
		//delay(1000);
		
		if(rec_data != 0xFF) 
		{
			switch (rec_data) {
				case 0x00:
					status = 0;
					LCD_Clear();
				break;
				case 0x01:
					status = 1;
				break;
				case 0x02:
					if(unit == 'C') {
						eeprom_write_data(0x00, 'F');
						unit = 'F';
					}
					else {
						eeprom_write_data(0x00, 'C');
						unit = 'C';
					}
				break;
				default:
				break;
			}
			rec_data = 0xFF;
		}
	}


}